<template>
  <div style="width: 100%">
    <div id="deawer">
      <v-tableTitle
        :btnConfig="btnConfig"
        @cancel="cancel"
        @save="save"
        @allocate="allocate"
        @deliver="deliver"
        @delete1="delete1"
        @task="task"
      ></v-tableTitle>
      <div class="detailBox">
        <v-detail ref="detail"></v-detail>
      </div>
      <div class="detailListBox">
        <v-detailList ref="detailList"></v-detailList>
      </div>
    </div>
  </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import skipMixin from "@/mixins/SkipMixin";
import config from "../Config"
import tableTitle from "commons/TableTitle";
import detail from "../details/Detail"
import detailList from "../detailsList/DetailList"

export default {
  name: "",
  mixins: [baseMixin, skipMixin, config],
  data () {
    return {
      newFlag: false,
      routeData: {}
    };
  },
  computed: {
    btnConfig () {
      return [
        { label: this.$t('common.save'), certigier: !this.btnPermNameList.includes('WM05020310'), invalid: "save" },
        {
          label: this.$t('out.action'), type: "more", certigier: !this.btnPermNameList.includes('WM05020311'), btnConfig: [
            { label: this.$t('out.allocation'), certigier: this.allocateStatus||!this.btnPermNameList.includes('WM05020312'), invalid: "allocate" },
            { label: this.$t('out.revocation'), certigier: this.allocateStatus||!this.btnPermNameList.includes('WM05020313'), invalid: "deliver" },
            { label: this.$t('wave.filterorder'), certigier: this.allocateStatus||!this.btnPermNameList.includes('WM05020314'), invalid: "delete1" },
            { label: this.$t('wave.taskdown'), certigier: this.allocateStatus||!this.btnPermNameList.includes('WM05020315'), invalid: "task" }
          ]
        },
        { label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM05020319'), invalid: "cancel" },
      ]
    }
  },
  created () {
  },
  methods: {
    async task () {
      try {
        let res = await this.$api.get(`/waveReleaseTask/${this.routeData.wavekey}`);
        
          this.$refs.detail.getData();
          this.$refs.detailList.$children[1].getTableData(1, {});
          this.$message.success(res.returnMgr);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async delete1 () {
      try {
        let res = await this.$api.get(`/waveEliminateOrders/${this.routeData.wavekey}`);
        
          this.$refs.detail.getData();
          this.$refs.detailList.$children[1].getTableData(1, {});
          this.$message.success(res.returnMgr);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async deliver () {
      try {
        let res = await this.$api.get(`/waveRevokeAllocate/${this.routeData.wavekey}`);
        
          this.$refs.detail.getData();
          this.$refs.detailList.$children[1].getTableData(1, {});
          this.$message.success(res.returnMgr);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async allocate () {
      try {
        let res = await this.$api.post(`/allocateByWavekey/${this.routeData.wavekey}/${this.$store.state.wms}`, this.routeData);
        
          this.$refs.detail.getData();
          this.$refs.detailList.$children[1].getTableData(1, {});
          this.$message.success(res.returnMgr);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    //保存按钮
    async save () {
      let flag = this.$refs.detail.$children[1].$refs.input.submitForm("from");
      if (!flag) {
        return;
      }
      let detail = this.$refs.detail.detailData;

      try {
        let res = await this.$api.post(`/updateWave`, detail);
        
          this.$message.success(res.returnMgr);
        
      } catch (e) {
        this.$message.error(e)
      }
    },
    //弹框取消
    cancel () {
      let routerName = JSON.parse(sessionStorage.getItem('routerName'));
      let num = -1;
      if (routerName) {
        num = routerName.indexOf("wavemgmt");
      }
      if (num === -1) {
        this.$router.push({ name: "wavemgmt" });
      } else {
        num = num - 0;
        routerName.splice(num, 1);
        sessionStorage.setItem('routerName', JSON.stringify(routerName));
        this.$router.push({ name: "home1" })
      }

    },
  },
  mounted () {
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.routeData = data.routeData;
      this.newFlag = data.newFlag;
    } else {
      this.routeData = this.$store.state.routeData;
      this.newFlag = this.$store.state.newFlag;
    }

  },
  watch: {},
  components: {
    "v-tableTitle": tableTitle,
    "v-detail": detail,
    "v-detailList": detailList,
  },
  beforeDestroy () {
    let routerName = JSON.parse(sessionStorage.getItem('routerName'));
    let num = -1;
    if (routerName) {
      num = routerName.indexOf("wavemgmt");
    }
    if (num === -1) {
    } else {
      num = num - 0;
      routerName.splice(num, 1);
      sessionStorage.setItem('routerName', JSON.stringify(routerName));
    }
  }
}
</script>

<style scoped>
</style>
